//
//  OthersViewController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "OthersViewController.h"

#import "FormattingUtilities.h"
#import "GSSCommunicator.h"
#import "GSSObject.h"

@implementation OthersViewController

- (void)viewDidLoad {
    self.showsAddButton = NO;
    [super viewDidLoad];
}


//MARK: -

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    if (aTableView != self.tableView)
        return [super tableView:aTableView numberOfRowsInSection:section];
    
    return [object.children count];    
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (aTableView != self.tableView)
        return [super tableView:aTableView cellForRowAtIndexPath:indexPath];

    static NSString *CellIdentifier = @"OthersViewCell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
        cell.textLabel.lineBreakMode = UILineBreakModeMiddleTruncation;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    }
    
    GSSObject *child = [[object.children sortedArrayUsingSelector:@selector(compare:)]
                        objectAtIndex:indexPath.row];
    BOOL stale = child.isStale;

    if (stale)
        [[GSSCommunicator defaultCommunicator] prefetchObject:child];
    
    cell.imageView.image = child.icon;
    cell.textLabel.text = child.name;
    
    if (!child.isPartial) {
        NSInteger count = [child.children count];
        NSString *files = (count == 1) ? LocalizedString(@"file") : LocalizedString(@"files");
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d %@", count, files];            
    } else {
        cell.detailTextLabel.text = nil;
    }
    
    return cell;
}

- (void)reloadData {
    NSUInteger count = [object.children count];
    
    if (count) {
        NSString *users = (count == 1) ? LocalizedString(@"user") : LocalizedString(@"users");
        self.footerTopLabel.text = [NSString stringWithFormat:@"%d %@", count, users];
    } else {
        self.footerTopLabel.text = LocalizedString(@"No users");
    }
 
    [self.tableView reloadData];
}

@end
